import { createRouter, createWebHashHistory } from "vue-router";
import Home from "../views/Home.vue";

const routes = [
    {
        path: '/',
        redirect: '/organization'
    }, {
        path: "/",
        name: "Home",
        component: Home,
        children: [
            {
                path: "/organization",
                name: "organization",
                meta: {
                    title: '组织管理'
                },
                component: () => import( /* webpackChunkName: "dashboard" */ "../views/OrganizationManage.vue")
            },
            {
                path: '/league',
                name: 'league',
                meta: {
                    title: '联盟管理'
                },
                component: () => import(/* webpackChunkName: "404" */ '../views/LeagueManage.vue')
            },{
                path: '/404',
                name: '404',
                meta: {
                    title: '找不到页面'
                },
                component: () => import(/* webpackChunkName: "404" */ '../views/404.vue')
            }, {
                path: '/403',
                name: '403',
                meta: {
                    title: '没有权限'
                },
                component: () => import(/* webpackChunkName: "403" */ '../views/403.vue')
            }, {
                path: '/user',
                name: 'user',
                meta: {
                    title: '个人中心'
                },
                component: () => import(/* webpackChunkName: "user" */ '../views/User.vue')
            }, {
                path: '/orderer',
                name: 'orderer',
                meta: {
                    title: 'Orderer节点'
                },
                component: () => import('../views/OrdererNode.vue'),
            }, {
                path: '/ordererConsole',
                name: 'ordererConsole',
                meta: {
                    title: 'Orderer节点控制台'
                },
                component: () => import('../views/OrdererNodeConsole.vue'),
            }, {
                path: '/peer',
                name: 'peer',
                meta: {
                    title: 'Peer节点'
                },
                component: () => import('../views/PeerNode.vue')
            }, {
                path: '/peerConsole',
                name: 'peerConsole',
                meta: {
                    title: 'Peer节点控制台'
                },
                component: () => import('../views/PeerNodeConsole.vue')
            }, {
                path: '/channel',
                name: 'channel',
                meta: {
                    title: '通道(channel)节点'
                },
                component: () => import('../views/ChannelManage.vue')
            }, {
                path: '/list',
                name: 'list',
                meta: {
                    title: '链码节点'
                },
                component: () => import('../views/ListCode.vue')
            }, {
                path: "/login",
                name: "login",
                meta: {
                    title: '登录'
                },
                component: () => import( /* webpackChunkName: "login" */ "../views/Login.vue")
            }
        ]
    }
];
const router = createRouter({
    history: createWebHashHistory(),
    routes
});

router.beforeEach((to, from, next) => {
    document.title = `${to.meta.title} | vue-manage-system`;
    const role = localStorage.getItem('ms_username');
    if (!role && to.path !== '/login') {
        next('/login');
    } else if (to.meta.permission) {
        // 如果是管理员权限则可进入，这里只是简单的模拟管理员权限而已
        role === 'admin'
            ? next()
            : next('/403');
    } else {
        next();
    }
});

export default router;